package com.laiyizhan.common.utils;

import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.util.Base64Utils;

/**
 * 可逆加密解密工具类, 需要Spring工具包
 */
public class SecurityTool {

    /** 密钥 */
    public static final String encryptKey = "laiyizhan@abc!@#";

    private static BasicTextEncryptor basicTextEncryptor;

    static {
        basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword(encryptKey);
    }

    /**
     * 加密(经过UrlEncode)
     *
     * @param value
     * @return
     */
    public static String encrypt(Object value) {
        if (value == null) {
            return "";
        }
        String encryptText = basicTextEncryptor.encrypt(String.valueOf(value));
        return Base64Utils.encodeToUrlSafeString(encryptText.getBytes());
    }


    /**
     * 解密
     *
     * @param texts
     * @return
     */
    public static String decrypt(String texts) {
        if (texts == null || texts.trim().equals("")) {
            return "";
        }
        byte[] text = Base64Utils.decodeFromUrlSafeString(texts);
        return basicTextEncryptor.decrypt(new String(text));
    }

}
